<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> 品牌份额异动</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">

    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>

    <script type="text/javascript" src="/webapp/js/common/jquery.global.settings.js"></script>

    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>


</head>
<body>
     <div class="easyui-container-fluid">
         <div class="easyui-panel" style="border:none;">
             <div class="nav" id="nav"></div>
             <div class="easyui-panel" style="border:none;">
                 <form id="ff">
                     <div style="margin-left:3px;">
                     	<span>
                         <label class="item-label">年份：</label>
                         <input type="text" class="easyui-combobox" name="year" id="year" style="width:100px;"/>
                     	</span>
                     	<span id="monthLabel">
	                         <label class="item-label" style="width: 50px;margin-left: 20px;">月份：</label>
	                         <input type="text" class="easyui-combobox" name="month" id="month" style="width:100px;"/></span>
                     	<span id="weekLabel">
	                         <label class="item-label" style="width: 50px;margin-left: 20px;">周数：</label>
	                         <input type="text" class="easyui-combobox" name="week" id="week" style="width:100px;"/></span>
                     	<label class="item-label" style="width: 50px;margin-left: 20px;">品类：</label>
		                <select id="categorySelect" name="categoryCode" class="easyui-combobox" style="width:100px;"></select>
                     	<label class="item-label">份额类型：</label>
		                <select  name="shareType" class="easyui-combobox" id="felx" data-options="panelHeight:'150'" style="width:80px;"  >
		                	<option value="COUNT">量份额</option>
		                	<option value="AMOUNT">额份额</option>
		                	<option value="APRICE">均价</option>
		                </select>

                         <label class="item-label">数据状态：</label>
                         <select id="comboSyayus" name="comboSyayus" class="easyui-combobox" data-options="panelHeight:'auto'" style="width:100px;" prompt="比较符">
                             <option value="ALL"  selected = "selected" >不限制</option>
                             <option value="COMBO">套餐</option>
                             <option value="NOCOMBO">非套餐</option>
                         </select>
                         <div style="display:inline-block;" id="categorywrapper">
                             <label class="item-label" style="width: 50px;margin-left: 20px;">规格：</label>
                             <input type="text" name="propCode" id="propCode" class="easyui-combobox" data-options="panelHeight:'150'" style="width:100px;" />
                             <select name="comparator" id="comparator"  class="easyui-combobox" data-options="panelHeight:'150'" style="width:50px;"   >
                                 <option value="EQUAL">=</option>
                                 <option value="GREAT">></option>
                                 <option value="GREAT_EQUAL">>=</option>
                                 <option value="LITTLE"><</option>
                                 <option value="LITTLE_EQUAL"><=</option>
                                 <option value="NOT_EQUAL"><></option>
                             </select>
                             <div style="display: inline-block" id="metadatavalue" >
                             </div>
                         </div>
                         <label class="item-label" style="width: 50px;pxmargin-left: 20px;">全年：</label>
                         <input id="isSalesAllYear" class="easyui-switchbutton" style="width:80px;height:30px;" >
                     </div>
                     <div style="margin-left:3px;margin-top:3px">
                         <label class="item-label" >商场码：</label>
                         <select id="mallComparer" name="mallComparer" class="easyui-combobox" data-options="panelHeight:'auto'" style="width:50px;" prompt="比较符">
                             <option value="EQUAL"  selected = "selected" >=</option>
                             <option value="NOT_EQUAL"><pre><></pre></option>
                         </select>
                         <input type="text" id="mallCode" name="mallCode" class="easyui-textbox" data-options="prompt: '多个用逗号隔开',panelHeight:'150'" style="width:10%;" />

                         <label class="item-label" >渠道码：</label>
                         <select id="channelComparer" name="channelComparer" class="easyui-combobox" data-options="panelHeight:'auto'" style="width:43px;" prompt="比较符">
                             <option value="EQUAL"  selected = "selected" >=</option>
                             <option value="NOT_EQUAL"><pre><></pre></option>
                         </select>
                         <input type="text" id="channelCode" name="channelCode" class="easyui-textbox" data-options="prompt: '多个用逗号隔开',panelHeight:'150'" style="width:10%;" />
                         <label class="item-label" style="width: 100px;pxmargin-left: 20px;">拼抖快型号：</label>
                         <input id="isExcludePdkSpecial" class="easyui-switchbutton" checked style="width:90px;height:30px;" onText="排除" offText="不排除" />

                         <a href="javascript:void(0);" style="width:80px;" class="easyui-linkbutton c8" onclick="searchBy()">查询</a>

                     </div>
                     <div style="margin-bottom:10px;margin-left: 10px;text-align:left;">
                     	<span id="totalInfo" style="margin-right:10px;font-weight: bold;font-size: 16px;"></span>
                     </div>
                 </form>
             </div>
             <div class="table-wrapper">
                 <table id="datagrid" class="easyui-datagrid" data-options="fitColumns:true,scrollbarSize:0"  style="max-height: 683px;">
                     <thead>
                        <tr>
                            <th data-options="field:'brandName',align:'center'" width="120" formatter="formatBrand">品牌</th>
                            <th data-options="field:'0',align:'center'" width="100" formatter="formatCount_0">本月（周）</th>
                            <th data-options="field:'1',align:'center'" width="100" formatter="formatCount_1">-1月（周）</th>
                            <th data-options="field:'2',align:'center'" width="100" formatter="formatCount_2">-2月（周）</th>
                            <th data-options="field:'3',align:'center'" width="100" formatter="formatCount_3">-3月（周）</th>
                            <th data-options="field:'4',align:'center'" width="100" formatter="formatCount_4">-4月（周）</th>
                            <th data-options="field:'5',align:'center'" width="100" formatter="formatCount_5">去年同期</th>
                            <th data-options="field:'8',align:'center'" width="100" formatter="formatCount_6">截止本期</th>
                            <th data-options="field:'6',align:'center'" width="100" formatter="formatCount_01">与上月份额</th>
                            <th data-options="field:'7',align:'center'" width="100" formatter="formatCount_05">与去年份额</th>
                        </tr>
                     </thead>
                 </table>
             </div>
         </div>
     </div>
     <script type="text/javascript">

         var checkedItems = "APRICE";
         $(function(){
             setNav();
             initUserCategorySelect("categorySelect",function (code) {
                 initModelSelect("propCode","metadatavalue",code);
                 // showRule();
             });
             initProvinceSelect("provinceSelect", provinceChange);
             initMonthWeek("monthLabel","weekLabel");
             $("#datagrid").datagrid({
                 // pagination:true,
                 rownumbers:true
             });
             aRecords("品牌份额异动","/webapp/pages/product/audit/brandshare")
         });
         function provinceChange(provinceCode){
        	initCitySelect("citySelect", provinceCode);
   			$.ajax({
   		        url:config.url+'/dictionary/getCityByProvince/'+provinceCode,
   		        type:"get",
   		        xhrFields:{withCredentials:true},
   		        success:function (data) {
   		            if(data){
   		                $(data).map(function(index,item){
   		                    item['text'] = item.name;
   		                });
   		                data.unshift({"text":"-请选择-","code":""});
   		                $("#citySelect").combobox({
   		                    "textFiled":"text",
   		                    "valueField":"code",
   		                    "data":data
   		                });
   		            }
   		        },
   		        error:function(XMLHttpRequest,textStatus,errorThrown){
   		        }
   		    })
         }
         
         function searchBy(){
             var data = getFormData(document.getElementById("ff"));
             data.reportType=window.parent.workMode.toUpperCase();
             data.mallCode=$("#mallCode").val();
             data.isSalesAllYear = $("#isSalesAllYear").switchbutton("options").checked ? 'true' : 'false';;
             data.isExcludePdkSpecial = $("#isExcludePdkSpecial").switchbutton("options").checked ? 'true' : 'false';
             /* if(data.propCode&&data.propValue&&data.comparator){
                  data.properties = [{code:data.propCode,type:data.comparator,value:data.propValue}];
                  data.comparator = data.comparator;
              }*/
             // alert(data.propValue);
             if(data.propValue){
                 data.propValue=data.propValue.toString();
             }

             // console.log(data)
             $.ajax({
                 url:config.url + "/brandShare/data",
                 type:"GET",
                 data:transfer(data),

                 dataType:'json',
                 xhrFields:{withCredentials:true},
                  beforeSend:function () {
                      $.messager.progress({
                          title:"请稍等",
                          interval:50,
                          text:"数据加载中..."
                      });
                    },
                  success:function(data){
                      $.messager.progress("close");
                      if (data.code && data.code == 515) {
                          $.messager.alert("提示", data.msg, "error");
                          return;
                      }
                      checkedItems=$("#felx").combobox("getValue");
                      var totalInfo = '';
                      if (data.data == null) {
                          $('#totalInfo').html(totalInfo);
                          $("#datagrid").datagrid({data: []}).datagrid("getPager");
                        return;
                      }
                	  if(Object.keys(data.data).length>0){
	                	  $("#datagrid").datagrid({data: data.data}).datagrid("getPager");
	                	  // $("#datagrid").datagrid({data: data.data}).datagrid("getPager");
                	  }else{
	                	  $("#datagrid").datagrid({data: []}).datagrid("getPager");
                	  }
                      var totalList = null;
                	  if('COUNT'==data.shareType){
                		  totalInfo += '销量数据统计----';
                          totalList =  data.allCount;
                	  }else if('AMOUNT'==data.shareType){
                		  totalInfo += '销额数据统计----';
                          totalList =  data.allTotal;
                	  }
                	  var period = window.parent.workMode=='week'?'周':'月';
                	  // console.log(data.total)
                	  if(totalList){
                          totalInfo += '本'+period+':' + totalList[0] + '&nbsp;&nbsp;';
                          totalInfo += '-1'+period+':' + totalList[1] + '&nbsp;&nbsp;';
                          totalInfo += '-2'+period+':' + totalList[2] + '&nbsp;&nbsp;';
                          totalInfo += '-3'+period+':' + totalList[3] + '&nbsp;&nbsp;';
                          totalInfo += '-4'+period+':' + totalList[4] + '&nbsp;&nbsp;';
                          totalInfo += '去年同期:' + totalList[5] + '&nbsp;&nbsp;';
                      }
                      var felx = $("#felx").combobox('getValue');
                      var isSalesAllYear = $("#isSalesAllYear").switchbutton("options").checked == true ? true : false;
                      if(felx=="COUNT" && isSalesAllYear){
                          totalInfo += '截止本期总量:' + data.allYearCount + '&nbsp;&nbsp;';
                      }else if (felx == "AMOUNT" && isSalesAllYear) {
                          totalInfo += '截止本期总额:' + data.allYearTotal + '&nbsp;&nbsp;';
                      }
                	  $('#totalInfo').html(totalInfo);
                  },
                  error:function(data){
                     console.log(data);
                     $.messager.progress("close");
                  }
              });
          }
        
        function formatBrand(val, row) {
 			return row.brandName + '(' + row.brandCode + ')';
 		}
		function formatCount_0(val, row) {
            if(checkedItems=="APRICE"){
			return row.shares[0];
            }else {
			return row.shares[0] + '%';
            }
		}
		function formatCount_1(val, row) {
            if(checkedItems=="APRICE"){
                return row.shares[1];
            }else{
                return row.shares[1] + '%';
            }
		}
		function formatCount_2(val, row) {
             if(checkedItems=="APRICE"){
			return row.shares[2];
             }else{
			return row.shares[2] + '%';
             }
		}
		function formatCount_3(val, row) {
             if(checkedItems=="APRICE"){
			return row.shares[3];
             }else{
			return row.shares[3] + '%';
             }
		}
		function formatCount_4(val, row) {
             if(checkedItems=="APRICE"){
			return row.shares[4];
             }else{
			return row.shares[4] + '%';
             }
		}
		function formatCount_5(val, row) {
             if(checkedItems=="APRICE"){
			return row.shares[5] ;
             }else {

			return row.shares[5] + '%';
             }
		}

		function formatCount_6(val, row) {
            var felx = $("#felx").combobox('getValue');
            var isSalesAllYear = $("#isSalesAllYear").switchbutton("options").checked == true ? true : false;
            if(felx=="COUNT" && isSalesAllYear){
                return row.allYearCountPer + '%(' + row.allYearCount + ')'
            }else if (felx == "AMOUNT" && isSalesAllYear) {
                return row.allYearTotalPer + '%(' + row.allYearTotal + ')';
            }
            return '--%(--)';
        }
		function formatCount_01(val, row) {
			if(row.shares[0]>row.shares[1]){
                if(checkedItems=="APRICE"){
				return style('+' + ~~(row.shares[0]-row.shares[1]), 'yes');
                }else{
				return style('+' + ~~(row.shares[0]*100-row.shares[1]*100)/100 + '%', 'yes');
                }
			}else if(row.shares[0]<row.shares[1]){
                if(checkedItems=="APRICE"){
				return style('-' + ~~(row.shares[1]-row.shares[0]), 'no');
                }else{
				return style('-' + ~~(row.shares[1]*100-row.shares[0]*100)/100 + '%', 'no');
                }
			}else{
				return '--';
			}
		}
		function formatCount_05(val, row) {
			if(row.shares[0]>row.shares[5]){
                if(checkedItems=="APRICE"){

				return style('+' + ~~(row.shares[0]-row.shares[5]), 'yes');
                }else{

				return style('+' + ~~(row.shares[0]*100-row.shares[5]*100)/100 + '%', 'yes');
                }
			}else if(row.shares[0]<row.shares[5]){
                if(checkedItems=="APRICE"){

				return style('-' + ~~(row.shares[5]-row.shares[0]), 'no');
                }else{

				return style('-' + ~~(row.shares[5]*100-row.shares[0]*100)/100 + '%', 'no');
                }
			}else{
				return '--';
			}
		}
		function style(text, flag){
			if('yes'==flag){
				return '<span style="font-size: 14px;font-weight: bold;color: red;">' + text + '</span>';
			}else if('no'==flag){
				return '<span style="font-size: 14px;font-weight: bold;color: green;">' + text + '</span>';
			}
			return text;
		}


         function initChannelSelectshare(channelSelectId, mallSelectId, initCallback){
             var dtd = $.Deferred();
             $.ajax({
                 url:config.url +"/channel/getAll",
                 type:"get",
                 xhrFields:{withCredentials:true},
                 success:function (data) {
                     if(data){
                         data.unshift({"name":"","code":""});
                         $("#"+channelSelectId).combobox({
                             "textField":"code",
                             "valueField":"code",
                             "panelHeight":"150",
                             "prompt":"--请选择--",
                             "data":data
                         });
                         dtd.resolve();
                         // 回调函数
                         if(typeof initCallback === "function"){
                             if(data.length>=2){
                                 $("#"+channelSelectId).combobox('select', data[1]['code']);
                             }
                             initCallback();
                         }
                     }
                 },
                 error:function(XMLHttpRequest,textStatus,errorThrown){
                     dtd.reject();
                 }
             });
             return dtd.promise();
         }
         $("#ff").keydown(function (event) {
             var e = event || window.event;
             var keyCode = e.keyCode || e.which;
             if(keyCode=="13"){
                 searchBy();
             }
         });
         //查询条件多选
         function initModelSelect(propCode,metadatawrapper,categoryCode,propValue){
             var dtd = $.Deferred();
             var dd = [];
             $.ajax({
                 url:config.url+"/categoryMetadata/getNameByCategoryCode?categoryCode="+categoryCode,
                 type:"get",
                 xhrFields:{withCredentials:true},
                 success:function (data) {
                     if(data){
                         var obj_map = {};
                         data.unshift({"name":"","code":""});
                         dd = data.map(function (item) {
                             obj_map[item.code] = item.values;
                         });
                         $("#"+propCode).combobox({
                             textField:"name",
                             valueField:"code",
                             prompt:"--请选择--",
                             panelHeight:"200px",
                             data:data,
                             onChange:function (newValue,oldValue) {
                                 var sub_data = obj_map[newValue];
                                 //根据是否有值来决定是下拉框还是文本框
                                 var inner = "";
                                 var proValueName = propValue || "propValue";
                                 if(sub_data){
                                     inner = "<select class='easyui-combobox'  data-options='multiple:true' name='"+proValueName+"' data-options=\"panelHeight:'auto'\" style='width:100px;' prompt='--请选择--'> <option></option>";
                                     if( sub_data == ""){
                                         inner += "<option value='true'>"+"是"+"</option>";
                                         inner += "<option value='false'>"+"否"+"</option>";
                                     }else{
                                         for(var i=0;i<sub_data.length;i++){
                                             inner += "<option >"+sub_data[i]+"</option>";
                                         }
                                     }
                                     inner += "</select>"
                                 }else{
                                     inner = "<input type='text' class='easyui-textbox' name='"+proValueName+"' style='width:100px' prompt='请输入值'/>"
                                 }
                                 $("#"+metadatawrapper).html(inner);
                                 $.parser.parse($("#"+metadatawrapper));
                             }
                         });
                         dtd.resolve();
                     }
                 },
                 error:function (XMLHttpRequest,textStatus,errorThrown) {
                     dtd.reject();
                 }
             });
             return dtd.promise();
         }
     </script>
</body>
</html>